#--*-- coding:utf-8 --*--

import time
from selenium import webdriver
# import requests
# from bs4 import BeautifulSoup
# import json
import re
from selenium.webdriver.common.action_chains import ActionChains
# import random

class Selenium_Taobao():

	driver = None

	def get_Qr(self):

		event = {}
		# 产生事件
		self.driver = webdriver.PhantomJS()
		# self.driver = webdriver.Firefox()

		

		self.driver.maximize_window()
		# 天猫
		# self.driver.get('https://login.taobao.com/member/login.jhtml?tpl_redirect_url=https%3A%2F%2Fwww.tmall.com%2F%3Fali_trackid%3D2%3Amm_26632360_8858797_53458628%3A1527586533_216_337212962%26clk1%3De4c63dd098d01d30afc0fde609597b57%26upsid%3De4c63dd098d01d30afc0fde609597b57&style=miniall&enup=true&newMini2=true&full_redirect=true&sub=true&from=tmall&allp=assets_css%3D3.0.10/login_pc.css&pms=1527586559322')  # 地址栏里输入网址
		
		# 淘宝
		action = ActionChains(self.driver)
		self.driver.get('https://login.taobao.com/member/login.jhtml?sub=true&style=miniall&from=subway&full_redirect=true&newMini2=true&tpl_redirect_url=//subway.simba.taobao.com/entry/login.htm')  # 地址栏里输入网址
		
		try:
			# 直接取
			Qr_img = self.driver.find_element_by_xpath('/html/body/div[1]/div/div/div[2]/div[4]/div/div[3]/div[1]/div[1]/img').get_attribute("src")
		except:

			#点击扫码登陆
			eme = self.driver.find_element_by_xpath('//*[@id="J_Static2Quick"]')
			action.move_to_element(eme).perform()
			action.move_by_offset(10,-10).perform()
			action.click().perform()

			time.sleep(2)
			Qr_img = self.driver.find_element_by_xpath('/html/body/div[1]/div/div/div[2]/div[4]/div/div[3]/div[1]/div[1]/img').get_attribute("src")

		# Qr_img = self.driver.find_element_by_id("J_QRCodeImg").find_element_by_tag_name("img").get_attribute("src")
		return {'qr':Qr_img,'instance':self.driver}
	
	

	# 新版软件获取cookies在视图函数里面，不在这里
	# def get_Cookie(self,instance):
	# 	time.sleep(1) #等待登陆完成 
	# 	if str(instance.current_url).split('=')[0] == 'https://www.tmall.com/?ali_trackid' or instance.current_url == 'https://myseller.taobao.com/home.htm' or instance.current_url == 'https://mai.taobao.com/seller_admin.htm' or instance.current_url == 'https://subway.simba.taobao.com/#!/home' or instance.current_url == 'https://subway.simba.taobao.com/#!/novice-village/index':
	# 	# if self.driver.current_url == 'https://subway.simba.taobao.com/#!/home':
	# 		cookies_dic = {}
	# 		cookies_list = instance.get_cookies()
	# 		for i in cookies_list:
	# 			print(i['domain'])
	# 			cookies_dic[i["name"]] = i["value"]
	# 		time.sleep(0.5)
	# 		instance.quit()
	# 		return cookies_dic
	# 	else:
	# 		instance.quit()
	# 		raise


if __name__ == '__main__':
	
	selenium_taobao = Selenium_Taobao()
	print (selenium_taobao.get_Qr())

#https://img.alicdn.com/tfscom/TB1pLedwmYTBKNjSZKbwu0J8pXa.png

